package com.bbs.comm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.bbs.model.User;


/**
 *存与单个用户有关的信息，与session绑定
 *
 */
public class SessionContext {
	private String userIP;
	private String loginTime;
	private HttpSession session=null;
	private User user;
	
	/**
	 * 注册新产生的session 在session监听器中用sessioncontext的实例调用
	 * @param session
	 */
	public void regist(HttpSession session){
		this.session=session;//sessioncontext中有session的引用  session 
		this.session.setAttribute(SysConstant.SESSION_CONTEXT, this); //session中放sessioncontext
	}
	/**
	 * 解除注册
	 */
	public void unregist(){
		this.session.removeAttribute(SysConstant.SESSION_CONTEXT);
	}
	/**
	 * 从获得ServletActionContext中获得request，然后得到session.sessioncontext的实例
	 * @return
	 */
	public static SessionContext getSessionContext(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = null;
		SessionContext context = null;
		if(request!=null){
			session = request.getSession(false);
			if(session!=null){
				context = (SessionContext) session.getAttribute(SysConstant.SESSION_CONTEXT);
			}
		}
		return context;
	}
	/**
	 * 从session中获取sessionContext
	 * @param session
	 * @return
	 */
	public static SessionContext getSessionContext(HttpSession session){
		SessionContext context = null;
		if(session!=null){
			context = (SessionContext) session.getAttribute(SysConstant.SESSION_CONTEXT);
		}
		return context;
	}
	public String getUserIP() {
		return userIP;
	}
	
	public void setUserIP(String userIP) {
		this.userIP = userIP;
	}
	
	public HttpSession getSession() {
		return session;
	}
	public void setSession(HttpSession session) {
		this.session = session;
	}

	public String getLoginTime() {
		return loginTime;
	}
	public void setLoginTime(String loginTime) {
		this.loginTime = loginTime;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	
}
